import 'package:flutter/cupertino.dart';
import 'package:video_player/video_player.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/routers/jump_router.dart';
import 'package:tiktokadult/tools_base/image/custom_network_image.dart';

import '../../../hj_model/splash/ads_model.dart';
import '../../../tools_base/widget/net_image_widget.dart';

class ShortVideoAdWidget extends StatefulWidget {
  final VideoModel? mediaInfo;
  final int index;
  final bool? isCurrentPage;
  final Function(VideoPlayerController?)? callback;
  const ShortVideoAdWidget({
    super.key,
    this.mediaInfo,
    this.index = 0,
    this.isCurrentPage,
    this.callback,
  });

  @override
  State<StatefulWidget> createState() {
    return _ShortVideoAdWidgetState();
  }
}

class _ShortVideoAdWidgetState extends State<ShortVideoAdWidget> {
  VideoModel? get mediaInfo => widget.mediaInfo;

  int get index => widget.index;

  bool? get isCurrentPage => widget.isCurrentPage;

  AdsInfoModel? get adv => mediaInfo?.randomAdsInfo;

  @override
  void didUpdateWidget(covariant ShortVideoAdWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.isCurrentPage == true) {
      widget.callback!(null);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        pushToPageByLink(adv?.href, id: adv?.id);
      },
      child: Container(
        child: NetworkImageLoader(
          imageUrl: adv?.cover ?? "",
          borderRadius: 0,
        ),
      ),
    );
  }
}
